#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "cJSON.h"

void createJsonFile(char *filePath);
void parseJsonFile(char *filePath);
char *json_loader(char *path);

int main()
{
    printf("Hello World!\n");
//    createJsonFile("test.json");
    parseJsonFile("test.json");
    return 0;
}


void createJsonFile(char *filePath)
{
    cJSON *obj = cJSON_CreateObject();
    cJSON *subobj = cJSON_CreateObject();
    cJSON_AddItemReferenceToObject(subobj, "factory", cJSON_CreateString("一汽大众"));
    cJSON_AddItemReferenceToObject(subobj, "last", cJSON_CreateNumber(31));
    cJSON_AddItemReferenceToObject(subobj, "price", cJSON_CreateNumber(83));
    cJSON_AddItemReferenceToObject(subobj, "sell", cJSON_CreateNumber(49));
    cJSON_AddItemReferenceToObject(subobj, "sum", cJSON_CreateNumber(80));

    cJSON *objArr = cJSON_CreateArray();
    cJSON_AddItemReferenceToArray(objArr, cJSON_CreateNumber(123));
    cJSON_AddItemReferenceToArray(objArr, cJSON_CreateBool(1));
    cJSON_AddItemReferenceToArray(objArr, cJSON_CreateString("hello world"));

    cJSON *s_subobj = cJSON_CreateObject();
    cJSON_AddItemReferenceToObject(s_subobj, "梅赛德斯奔驰", cJSON_CreateString("心所向, 持以恒"));

    cJSON_AddItemReferenceToArray(objArr, s_subobj);
    cJSON_AddItemReferenceToObject(subobj, "other", objArr);
    cJSON_AddItemReferenceToObject(obj, "奔驰", subobj);

    char *str = cJSON_Print(obj);
    FILE *file = fopen(filePath, "w");
    fwrite(str, sizeof (char), strlen(str), file);
    fclose(file);
}

void parseJsonFile(char *filePath)
{
    cJSON *json = NULL;
    char *out = NULL;
    char *line = json_loader(filePath);

    json = cJSON_Parse(line); //获取整个大的句柄
    out = cJSON_Print(json);  //这个是可以输出的。为获取的整个json的值
    printf("%s\n", out);

    cJSON *objItem = cJSON_GetObjectItem(json,"奔驰"); //获取这个对象成员
    cJSON *subobjItem = cJSON_GetObjectItem(objItem,"factory"); //获取这个对象成员
    if (subobjItem->type == cJSON_String)
    {
        printf("factory:%s\n",subobjItem->valuestring);
    }

    subobjItem = cJSON_GetObjectItem(objItem,"last");
    if (subobjItem->type == cJSON_Number)
    {
        printf("last:%d\n",subobjItem->valueint);
    }

    subobjItem = cJSON_GetObjectItem(objItem,"price");
    if (subobjItem->type == cJSON_Number)
    {
        printf("price:%d\n",subobjItem->valueint);
    }

    subobjItem = cJSON_GetObjectItem(objItem,"sell");
    if (subobjItem->type == cJSON_Number)
    {
        printf("sell:%d\n",subobjItem->valueint);
    }

    subobjItem = cJSON_GetObjectItem(objItem,"sum");
    if (subobjItem->type == cJSON_Number)
    {
        printf("sum:%d\n",subobjItem->valueint);
    }

    subobjItem = cJSON_GetObjectItem(objItem,"other");
    if (subobjItem->type == cJSON_Array)
    {
        cJSON *item = cJSON_GetArrayItem(subobjItem, 0);
        if (item->type == cJSON_Number)
        {
            printf("other: [%d  ",item->valueint);
        }

        item = cJSON_GetArrayItem(subobjItem, 1);
        if (item->type == cJSON_True || item->type == cJSON_False)
        {
            printf("%d  ",item->valueint);
        }

        item = cJSON_GetArrayItem(subobjItem, 2);
        if (item->type == cJSON_String)
        {
            printf("%s  ",item->valuestring);
        }

        item = cJSON_GetObjectItem(cJSON_GetArrayItem(subobjItem, 3), "梅赛德斯奔驰");
        if (item->type == cJSON_String)
        {
            printf("%s : ",item->string);
            printf("%s]\n",item->valuestring);
        }
    }

    if (NULL != out)
    {
        free(out);
    }
    cJSON_Delete(json);
}

char *json_loader(char *path)
{
    FILE *file = fopen(path,"rb");
    long len;
    char *content;

    fseek(file, 0, SEEK_END);
    len = ftell(file);
    fseek(file, 0, SEEK_SET);
    content = (char*)malloc(len + 1);
    if (content == NULL)
    {
        perror("no enough space:");
        exit(1);
    }
    fread(content, sizeof (char), len, file);
    fclose(file);
    return content;
}
